EXPORT_ASSERT_TO_GLOBALS = true
package.path = package.path ..';../?.lua'
require('lib/luaunit')
local Client = require('CSmsg/clientModule')
_G.testMsgModule = {} --class
require('socket')

function testMsgModule:setUp()
    -- set up tests
    -- self.args = 1
    -- print( 'testMsgModule:setUp' )
    os.execute('lua5.1 CSmsg/server.lua 127.0.0.1 12345 &')
    socket.select(nil, nil, 0.2)
    -- os.execute('lua5.1 CSmsg/serverGame.lua &')

    self.client = Client:New()
    local host = "127.0.0.1"
    local port = 12345
    local timeout = 0
    self.client:connect(host, port, timeout)

end

function testMsgModule:tearDown()
    -- some tearDown() code if necessary
    -- print( 'testMsgModule:tearDown' )
    print('kill lua server')
    os.execute('pkill -9 lua')
end

-- function testMsgModule:testMsgBackRevert()
--     -- arrange
--     local msg1 = "questGameID"
--     local msg2 = "questBuffID"
--     local msgBack1
--     -- act
--     self.client:request(msg1..'\n')
--     self.client.onReceived = function (client, msg)
--         print(">>>>>>>onReceived r1ewrite")
--         print(client)
--         print(msg)
--         msgBack1 = msg
--     end
--     for i=1,10,1 do
--         print(">>>>>>>>>>testMsgBackRevert")
--         print(i)
--         self.client:onUpdate()
--         if msgBack1 then
--             break
--         end
--     end
--     -- assert
--     assertEquals( msgBack1, msg1 )
-- end

function testMsgModule:testMsgFuncFlowMsgWithCallBack()
    -- arrange
        --TODO: root request with a message 2017年12月04日 星期一 16时34分05秒
        --decode the meg back get func and value
        --call func with value
        local game = {}
        game.dwRoleID = 0
        local msg = "requestRoleInfo(10003)"..'\n'
        self.client:request(msg, function (roleid)
            print(">......callBack",roleid)
            game.dwRoleID = roleid
        end)
        self.client.onReceived = function(client, msg)
            print(">....onReceived ...........", msg)
            local func , values = self.client:decodeMsg(msg)
            if func then
                func(values)
            end
        end
    -- act
        for i=1, 3 ,1 do
            print(">>>>>>>testMsgFuncFlowMsgWithCallBack")
            print(i)
            self.client:onUpdate()
        end
    -- assert
    local dwRoleIDExpect = "8155676"
    assertEquals(game.dwRoleID , dwRoleIDExpect)
end

-- function test_csmsg:testMsgCoroutine()
--     -- arrange
--     local msg1 = "questGameID"
--     local msg2 = "questBuffID"
--     local msgBack1
--     -- act
--     co = coroutine.create(function(a1, a2)
--         runAsyncFunc(requestLocalDup, playerID)
--         runAsyncFunc(createDup, dupid)
--     end)
--     co.resume(co,playerID)
--     co.resume(co)
--     -- assert
--     assertEquals(  )
-- end

-- function runAsyncFunc( func, ... )
-- 	local current = coroutine.running
-- 	func(function (  )
-- 		coroutine.resume(current)
-- 	end, ...)
-- 	coroutine.yield()
-- end

lu = LuaUnit.new()
lu:setOutputType("tap")
os.exit( lu:runSuite() )
